{include file="../include.tpl"}
<body>
{literal}
<script>
$(function() {
		$( "input[type=submit], a, button" )
			.button()
			.click(function(event) {
			    var bid = $(event.target)[0].id;
			    $("#ui-id-1").html("创建用户");
			    $("#name").val("");

			    if(bid=="createUser")
			    {
			        $("#selMenus")[0].disabled=false;
			        $("#name")[0].disabled=false;
			    	$.get("?act=admin&op=ajaxGetRoles",function(data)
			    	{
			    	    //alert(data);
			    	    var json = eval("("+data+")");
			    	    $("#selMenus").html("<option value='0'>选择</option>");
			    	    for(i=0;i<json.length;i++)
			    	    {
			    	        if(json[i].isSel==1)
			    	        {
			    	        	$("#selMenus").append("<option value='"+json[i].id+"' selected>"+json[i].name+"</option>");
			    	        }
			    	        else
			    	        {
			    	        	$("#selMenus").append("<option value='"+json[i].id+"'>"+json[i].name+"</option>");
			    	        }	
			    	    }
			    	    $("#dialog-form").dialog("open");
			    	});
			    }
			    else if(bid.indexOf("editUser")!=-1)
			    {
			        //alert("mmmm");
			        var id = bid.replace("editUser","");
			        $("#editId").val(id);
			        $("#ui-id-1").html("编辑用户");
			        $("#name").val($("#tdName"+id).html());
			        $("#pass").val("");
			        var roleId = $("#hidRoleId"+id).val();
			    	$.get("?act=admin&op=ajaxGetRoles&id="+roleId,function(data)
			    	{
			    	    //alert(data);
			    	    $("#selMenus").html("");
			    	    var json = eval("("+data+")");
			    	    for(i=0;i<json.length;i++)
			    	    {
			    	        if(json[i].isSel=="1")
			    	        {
			    	        	$("#selMenus").append("<option value='"+json[i].id+"' selected>"+json[i].name+"</option>");
			    	        }
			    	        else
			    	        {
			    	        	$("#selMenus").append("<option value='"+json[i].id+"'>"+json[i].name+"</option>");
			    	        }
			    	    }
			    	    if(id==1)
			        	{
			           		$("#selMenus")[0].disabled=true;
			           		$("#name")[0].disabled=true;
			       	 	}
			       	 	else
			       	 	{
			           		$("#selMenus")[0].disabled=false;
			           		$("#name")[0].disabled=false;
			       	 	}
			       	 	$("#selMenus").change();
			    	    $("#dialog-form").dialog("open");
			    	});
			    }
			    else if(bid.indexOf("delUser")!=-1)
			    {
			        //alert("mmmm");
			        if(confirm("确认删除吗？"))
			        {
						var id = bid.replace("delUser","");
			    		$.get("?act=admin&op=ajaxDelAdmin&id="+id,function(data)
			    		{
			    	    	var json = eval("("+data+")");
			    	    	if(json.st=="ok")
			    	    	{
			    	    	    alert(json.msg);
			    	    	    window.location=window.location;
			    	    	}
			    	    	else if(json.st=="err")
			    	    	{
			    	    	    alert(json.msg);
			    	    	}
			    		});
			        }
			    }
			    else if(bid.indexOf("banUser")!=-1)
			    {
			        //alert("mmmm");
			        var ban = 0;
			        if($(event.target)[0].innerHTML.indexOf("停用")!=-1)
			        {
			             if(!confirm("确认停用吗？"))
			             {
			                return;
			             }
			             ban = 1;
			        }
			        if($(event.target)[0].innerHTML.indexOf("启用")!=-1)
			        {
			             if(!confirm("确认启用吗？"))
			             {
			                return;
			             }
			             ban = 0;
			        }
			        //alert(ban);
                    var id = bid.replace("banUser","");
			    	$.get("?act=admin&op=ajaxBanAdmin&id="+id+"&ban="+ban,function(data)
			    	{
			    	    var json = eval("("+data+")");
			    	    if(json.st=="ok")
			    	    {
			    	    	alert(json.msg);
			    	    	window.location=window.location;
			    	    }
			    	    else if(json.st=="err")
			    	    {
			    	    	alert(json.msg);
			    	    }
			    	});
			      
			    }
		});
		$( "#dialog-form" ).dialog({
			autoOpen: false,
			height: 300,
			width: 380,
			modal: true,
			buttons: {
				"保存": function() {
					var bValid = true;
                    if($("#name").val()=="")
                    {
                       $("#td_msg_name").html("帐号为必填项");
                       bValid = false;
                    }
                    if($("#pass").val()=="")
                    {
                       $("#td_msg_pass").html("密码为必填项");
                       bValid = false;
                    }
                    var rolRelId = 0;
                    var rolRelName = "";
                    if($("#selMenus").val()=="3")
                    {
                        if($("#selMaker").val()=="0")
                        {
                            $("#td_msg_maker").html("厂商为必选项");
                            bValid = false;
                        }
                        rolRelId = $("#selMaker").val();
                        rolRelName = $("#selMaker").find("option:selected").text();
                    }
                    else
                    if($("#selMenus").val()=="4")
                    {
                        if($("#selMedia").val()=="0")
                        {
                            $("#td_msg_media").html("媒体为必选项");
                            bValid = false;
                        }
                        rolRelId = $("#selMedia").val();
                        rolRelName = $("#selMedia").find("option:selected").text();
                    }
					if(bValid ){
					    var obj = $(this);
					    var roleName = $("#selMenus").find("option:selected").text()
						$.post("?act=admin&op=ajaxSaveUser&id="+$("#editId").val(),{name:$("#name").val(),roleId:$("#selMenus").val(),roleName:roleName,pass:$("#pass").val(),roleRelId:rolRelId,roleRelName:rolRelName},function(data)
						{
						    var json = eval("("+data+")");
						    if(json.st=="ok")
						    {
								alert("保存成功");
								obj.dialog( "close" );
								window.location=window.location;
							}
							else if(json.st=="err1")
							{
								alert("保存失败：已经存在");
							}
							else if(json.st=="err2")
							{
								alert("保存失败：用户不能为空");
							}
						}); 
					}
				},
				"取消": function() {
					$(this).dialog( "close" );
				}
			},
			close: function() {
				
			}
		});
		$("#selMenus").change(function(){
		   if($(this).val()=="3")
		   {
				$.get("?act=game&op=ajaxGetMakers",function(data)
			    	{
			    	    //alert(data);
			    	    var json = eval("("+data+")");
			    	     $("#selMaker").html("<option value='0'>选择</option>");
			    	    for(i=0;i<json.length;i++)
			    	    {
			    	        $("#selMaker").append("<option value='"+json[i].id+"'>"+json[i].name+"</option>");
			    	    }
			    });
		       $("#trFormMaker").show();
		       $("#trFormMedia").hide();
		   }
		   else if($(this).val()=="4")
		   {
				$.get("?act=media&op=ajaxGetMedias",function(data)
				{
			 		//alert(data);
			  		var json = eval("("+data+")");
			  		$("#selMedia").html("<option value='0'>选择</option>");
			  		for(i=0;i<json.length;i++)
			  		{
			      		$("#selMedia").append("<option value='"+json[i].id+"'>"+json[i].name+"</option>");
			  		}
			  		//$("#selMedia").append("<option value='-1'>其他</option>");
				});
		   	   $("#trFormMedia").show();
		   	   $("#trFormMaker").hide();
		   }
		   else
		   {
		   	   $("#trFormMedia").hide();
		   	   $("#trFormMaker").hide();
		   }
		   
		});
});
</script>
{/literal}
<div id="dialog-form" title="创建用户">
	<form>
<fieldset>
       <table >
       <tr><td height="40px">帐号：</td><td><input type="text" name="name" id="name" class="text ui-widget-content ui-corner-all" style="width:200px;"/></td><td id="td_msg_name" class="msg_txt"></td></tr>
       <tr><td height="40px">密码：</td><td><input type="text" name="pass" id="pass" class="text ui-widget-content ui-corner-all" style="width:200px;"/></td><td id="td_msg_pass" class="msg_txt"></td></tr>
       <tr><td height="40px">角色：</td><td>
       <select name="selMenus" id="selMenus" class="text ui-widget-content ui-corner-all" style="width:200px;height:25px;">
 </select></td><td class="msg_txt"></td></tr>
       <tr style="display:none;" id="trFormMedia"><td >媒体：</td><td>
       <select name="selMedia" id="selMedia" class="text ui-widget-content ui-corner-all" style="width:200px;height:25px;">
 </select></td><td class="msg_txt" id="td_msg_media"></td></tr>
       <tr style="display:none;" id="trFormMaker"><td >厂商：</td><td>
       <select name="selMaker" id="selMaker" class="text ui-widget-content ui-corner-all" style="width:200px;height:25px;">
 </select></td><td class="msg_txt" id="td_msg_maker"></td></tr>
       </table>
</fieldset>
	<input type="hidden" id="editId" value=""/>
	</form>
</div>
<table width="100%" border="0" cellpadding="0" cellspacing="0">
  <tr>
    <td width="17" height="29" valign="top" background="images/mail_leftbg.gif"><img src="images/left-top-right.gif" width="17" height="29" /></td>
    <td height="29" valign="top" background="images/content-bg.gif"><table width="100%" height="31" border="0" cellpadding="0" cellspacing="0" class="left_topbg" id="table2">
      <tr>
        <td height="31"><div class="titlebt">用户管理</div></td>
      </tr>
    </table></td>
    <td width="16" valign="top" background="images/mail_rightbg.gif"><img src="images/nav-right-bg.gif" width="16" height="29" /></td>
  </tr>
  <tr>
    <td height="71" valign="middle" background="images/mail_leftbg.gif">&nbsp;</td>
    <td valign="top" bgcolor="#F7F8F9"><table width="100%" height="138" border="0" cellpadding="0" cellspacing="0">
      <tr>
        <td height="13" valign="top">&nbsp;</td>
      </tr>
      <tr>
        <td valign="top"><table width="98%" border="0" align="center" cellpadding="0" cellspacing="0">
          <tr>
            <td class="left_txt">当前位置：用户管理</td>
          </tr>
          <tr>
            <td height="20"><table width="100%" height="1" border="0" cellpadding="0" cellspacing="0" bgcolor="#CCCCCC">
              <tr>
                <td></td>
              </tr>
            </table></td>
          </tr>
          <tr>
            <td><table width="100%" height="55" border="0" cellpadding="0" cellspacing="0">
              <tr>
                <td width="10%" height="55" valign="middle"><img src="images/title.gif" width="54" height="55"></td>
                <td width="90%" valign="top"><span class="left_txt2">在这里，您可以创建或删除后台用户用户</span>
                <br/><button id="createUser">创建用户</button></td>
              </tr>
            </table></td>
          </tr>
		  <tr>
            <td height="20"><table width="100%" height="1" border="0" cellpadding="0" cellspacing="0" bgcolor="#CCCCCC">
              <tr><td></td></tr></table>
            </td>
          </tr>
          <tr>
            <td>
            <div id="users-contain" class="ui-widget">
	<h1>用户列表:</h1>
	<table id="users" class="ui-widget ui-widget-content">
		<thead>
			<tr class="ui-widget-header ">
				<th width="60px">编号</th>
				<th width="130px">帐号</th>
				<th width="180px">角色</th>
				<th>操作</th>
			</tr>
		</thead>
		<tbody>
              {section name=sec1 loop=$data}
              <tr>
              <td>{$data[sec1].disId}</td>
              <td id="tdName{$data[sec1].id}">{$data[sec1].name}</td>
              <td id="tdRole{$data[sec1].id}">{$data[sec1].role}</td>
              <td><!--<button id="editUser{$data[sec1].id}">编辑</button>&nbsp;&nbsp;&nbsp;&nbsp;--><button id="banUser{$data[sec1].id}">{if $data[sec1].isInvalid eq 0}停用{else}<启用>{/if}</button>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<button id="delUser{$data[sec1].id}">删除</button></td>
              </tr>
              <input type="hidden" id="hidRoleId{$data[sec1].id}" value="{$data[sec1].roleId}"/>
              <input type="hidden" id="hidRoleRelId{$data[sec1].id}" value="{$data[sec1].roleRelId}"/>
              {/section}
		</tbody>
	</table>
</div>
 </td>
          </tr>
        </table>
          </td>
      </tr>
    </table></td>
    <td background="images/mail_rightbg.gif">&nbsp;</td>
  </tr>
  <tr>
    <td valign="middle" background="images/mail_leftbg.gif"><img src="images/buttom_left2.gif" width="17" height="17" /></td>
      <td height="17" valign="top" background="images/buttom_bgs.gif"><img src="images/buttom_bgs.gif" width="17" height="17" /></td>
    <td background="images/mail_rightbg.gif"><img src="images/buttom_right2.gif" width="16" height="17" /></td>
  </tr>
</table>
</body>
